{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 函数\n",
    "- 代码的一种组织形式\n",
    "- 一个函数一般完成一项特定的功能\n",
    "- 函数使用\n",
    "    - 函数需要先定义\n",
    "    - 使用函数，俗称调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义一个函数\n",
    "# 只定义的话不会执行\n",
    "# 1、def关键字，后跟一个空格\n",
    "# 2、函数名，自己定义。起名遵循命名规则，约定俗称，大驼峰命名只给类用\n",
    "# 3、后面括号和冒号不能省略，括号内可以有参数\n",
    "# 4、函数内所有代码缩进\n",
    "\n",
    "def func():\n",
    "    print(\"我是一个函数\")\n",
    "    print(\"我要完成一个功能\")\n",
    "    print(\"我要结束啦\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我是一个函数\n",
      "我要完成一个功能\n",
      "我要结束啦\n"
     ]
    }
   ],
   "source": [
    "# 函数调用\n",
    "# 直接函数名后跟括号\n",
    "func()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 函数的参数和返回值\n",
    "- 参数：负责给函数传递一些必要的数据或者信息\n",
    "    - 形参（形式参数）：在函数定义的时候用到的参数没有具体值，只是一个占位的符号，称为形参\n",
    "    - 实参（实际参数）：在调用函数时，输入的值\n",
    "- 返回值：函数的执行结果\n",
    "    - 使用return关键字\n",
    "    - 如果没有return,默认返回一个None\n",
    "    - 函数一旦执行return,则无条件返回,即结束函数的执行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "明月,你怎么了？\n",
      "Sir，你不理某某就走啊\n"
     ]
    }
   ],
   "source": [
    "# 参数的定义和使用\n",
    "# 参数person只是一个符号，代表的调用的时候的某一个数据\n",
    "# 调用时，会用p的值代替函数中所有的person\n",
    "def hello(person):\n",
    "    print(\"{0},你怎么了？\".format(person))\n",
    "    print(\"Sir，你不理某某就走啊\")\n",
    "\n",
    "p = \"明月\"\n",
    "hello(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "明月,你怎么了？\n",
      "Sir，你不理某某就走啊\n",
      "已经跟明月打完招呼了,他不理我\n"
     ]
    }
   ],
   "source": [
    "# return语句的基本使用\n",
    "# 函数打完招呼后,返回一句话\n",
    "\n",
    "def hello(person):\n",
    "    print(\"{0},你怎么了？\".format(person))\n",
    "    print(\"Sir，你不理某某就走啊\")\n",
    "    return \"已经跟{0}打完招呼了,他不理我\".format(person)\n",
    "\n",
    "p = \"明月\"\n",
    "rst = hello(p)\n",
    "print(rst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "明月,你怎么了？\n",
      "我提前结束了!!!\n"
     ]
    }
   ],
   "source": [
    "# return案例2\n",
    "\n",
    "\n",
    "def hello(person):\n",
    "    print(\"{0},你怎么了？\".format(person))\n",
    "    return \"我提前结束了!!!\".format(person)\n",
    "    print(\"Sir，你不理某某就走啊\")\n",
    "\n",
    "p = \"明月\"\n",
    "rst = hello(p)\n",
    "print(rst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 \n",
      "2 4 \n",
      "3 6 9 \n",
      "4 8 12 16 \n",
      "5 10 15 20 25 \n",
      "6 12 18 24 30 36 \n",
      "7 14 21 28 35 42 49 \n",
      "8 16 24 32 40 48 56 64 \n",
      "9 18 27 36 45 54 63 72 81 \n"
     ]
    }
   ],
   "source": [
    "# 九九乘法表\n",
    "# version 1.0\n",
    "# range函数,包含左侧,不包含右侧数值\n",
    "for row in range(1,10):\n",
    "    for cloumn in range(1 , row + 1):\n",
    "        # print函数默认任务打印完毕后换行\n",
    "        print(row * cloumn,end =\" \")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function print in module builtins:\n",
      "\n",
      "print(...)\n",
      "    print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n",
      "    \n",
      "    Prints the values to a stream, or to sys.stdout by default.\n",
      "    Optional keyword arguments:\n",
      "    file:  a file-like object (stream); defaults to the current sys.stdout.\n",
      "    sep:   string inserted between values, default a space.\n",
      "    end:   string appended after the last value, default a newline.\n",
      "    flush: whether to forcibly flush the stream.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 查找函数帮助文档\n",
    "# 1.用help函数\n",
    "help(print)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 \n",
      "2 4 \n",
      "3 6 9 \n",
      "4 8 12 16 \n",
      "5 10 15 20 25 \n",
      "6 12 18 24 30 36 \n",
      "7 14 21 28 35 42 49 \n",
      "8 16 24 32 40 48 56 64 \n",
      "9 18 27 36 45 54 63 72 81 \n"
     ]
    }
   ],
   "source": [
    "# 九九乘法表\n",
    "# version 2.0 \n",
    "def printLine(row):\n",
    "    for cloumn in range(1 , row + 1):\n",
    "        print(row * cloumn,end =\" \")\n",
    "    print()\n",
    "\n",
    "for row in range(1,10):\n",
    "    printLine(row)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 参数详解\n",
    "- [参考资料](https://www.cnblogs.com/bingabcd/p/6671368.html)\n",
    "- python参考资料: headfirst python -->零基础入门学习Python(小甲鱼)-->后期可以考虑腾讯公开免费课\n",
    "- 参数分类\n",
    "    - 普通参数\n",
    "    - 默认参数\n",
    "    - 关键字参数\n",
    "    - 收集参数\n",
    "    \n",
    "- 普通参数\n",
    "    - 参考上例\n",
    "    - 定义的时候直接定义变量名\n",
    "    - 调用的时候直接把变量或值放入到指定位置\n",
    "    \n",
    "- 默认参数\n",
    "    - 形参带有默认值\n",
    "    - 调用的时候,如果没有相对应的形参赋值,则使用默认值\n",
    "    \n",
    "            def func_name(p1=v1,p2=v2....):\n",
    "               func_block\n",
    "            \n",
    "            #调用\n",
    "            func_name()\n",
    "            \n",
    "            #调用2\n",
    "            value1 = 100\n",
    "            value2 = 200\n",
    "            func_name(value1,value2)\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我是小明,年龄12,性别male\n",
      "我是小红,年龄12,性别女\n"
     ]
    }
   ],
   "source": [
    "# 默认参数示例\n",
    "def ref(name, age, gender = \"male\"):\n",
    "    print(\"我是{0},年龄{1},性别{2}\".format(name,age,gender))\n",
    "\n",
    "ref(\"小明\",12)\n",
    "ref(\"小红\",12,\"female\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
